using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    public class clsCHI_TIET_PYC
    {
        public clsCHI_TIET_PYC(string sConn)
        {
            SqlHelper.ConnectionString = sConn;
        }
        #region Private methods
        private CHI_TIET_PYC GetDataRow(DataRow row)
        {
            CHI_TIET_PYC objCHI_TIET_PYC = new CHI_TIET_PYC();
            try
            {
                if (row["MaChiTiet"] != DBNull.Value)
                    objCHI_TIET_PYC.MaChiTiet = int.Parse(row["MaChiTiet"].ToString());
            }
            catch { }
            try
            {
                if (row["MaThietBi"] != DBNull.Value)
                    objCHI_TIET_PYC.MaThietBi = ((row["MaThietBi"]).ToString());
            }
            catch { }
            try
            {
                if (row["MaPhieuYeuCau"] != DBNull.Value)
                    objCHI_TIET_PYC.MaPhieuYeuCau = (row["MaPhieuYeuCau"].ToString());
            }
            catch { }           
            try
            {
                if (row["TinhTrang"] != DBNull.Value)
                    objCHI_TIET_PYC.TinhTrang = int.Parse(row["TinhTrang"].ToString());
            }
            catch { }
            try
            {
                if (row["SoLuong"] != DBNull.Value)
                    objCHI_TIET_PYC.SoLuong = float.Parse(row["SoLuong"].ToString());
            }
            catch { }
            try
            {
                if (row["NgayGiaoHang"] != DBNull.Value)
                    objCHI_TIET_PYC.NgayGiaoHang = Convert.ToDateTime(row["NgayGiaoHang"]);
            }
            catch { }
            try
            {
                if (row["TenVTPT"] != DBNull.Value)
                    objCHI_TIET_PYC.TenVTPT = (row["TenVTPT"].ToString());
            }
            catch { }
            try
            {
                if (row["MaTrangThai"] != DBNull.Value)
                    objCHI_TIET_PYC.oTRANG_THAI_VTPT.MaTrangThai = int.Parse(row["MaTrangThai"].ToString());
            }
            catch { }

            try
            {
                if (row["TenTrangThai"] != DBNull.Value)
                    objCHI_TIET_PYC.oTRANG_THAI_VTPT.TenTrangThai = (row["TenTrangThai"].ToString());
            }
            catch { }

            try
            {
                if (row["ItemCategory"] != DBNull.Value)
                    objCHI_TIET_PYC.oTRANG_THAI_VTPT.ItemCategory = (row["ItemCategory"].ToString());
            }
            catch { }

            try
            {
                if (row["ValuationType"] != DBNull.Value)
                    objCHI_TIET_PYC.oTRANG_THAI_VTPT.ValuationType = (row["ValuationType"].ToString());
            }
            catch { }

            try
            {
                if (row["GhiChu"] != DBNull.Value)
                    objCHI_TIET_PYC.GhiChu = (row["GhiChu"].ToString());
            }
            catch { }

            try
            {
                if (row["MaNhaSanXuat"] != DBNull.Value)
                    objCHI_TIET_PYC.MaNhaSanXuat = (row["MaNhaSanXuat"].ToString());
            }
            catch { }

            try
            {
                if (row["DonViTinh"] != DBNull.Value)
                    objCHI_TIET_PYC.DonViTinh = (row["DonViTinh"].ToString());
            }
            catch { }

            return objCHI_TIET_PYC;
        }
        private List<CHI_TIET_PYC> GetCHI_TIET_PYC(DataTable dt)
        {
            List<CHI_TIET_PYC> lst = new List<CHI_TIET_PYC>();
            CHI_TIET_PYC obj;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                obj = this.GetDataRow(dt.Rows[i]);
                obj.STT = i + 1;
                lst.Add(obj);
            }
            return lst;
        }
        #endregion Private methods

        #region Public methods
        public DataTable usp_SelectCHI_TIET_PYC(string MaChiTiet)
        {
            string spName = "usp_SelectPHIEU_YEU_CAU";
            DataTable dt = new DataTable();

            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaChiTiet", MaChiTiet);

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, par);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, par);
                }

            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return dt;
        }
        public List<CHI_TIET_PYC> SelectCHI_TIET_PYC(string MaChiTiet)
        {
            List<CHI_TIET_PYC> lst = new List<CHI_TIET_PYC>();
            DataTable dt = new DataTable();
            dt = this.usp_SelectCHI_TIET_PYC(MaChiTiet);
            lst = this.GetCHI_TIET_PYC(dt);

            return lst;
        }

        public DataTable usp_SelectCHI_TIET_PYCByMaPhieuYeuCau(string MaPhieuYeuCau)
        {
            string spName = "usp_SelectCHI_TIET_PYCByMaPhieuYeuCau";
            DataTable dt = new DataTable();

            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaPhieuYeuCau", MaPhieuYeuCau);

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, par);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, par);
                }

            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return dt;
        }
        public List<CHI_TIET_PYC> SelectCHI_TIET_PYCByMaPhieuYeuCau(string MaPhieuYeuCau)
        {
            List<CHI_TIET_PYC> lst = new List<CHI_TIET_PYC>();
            DataTable dt = new DataTable();
            dt = this.usp_SelectCHI_TIET_PYCByMaPhieuYeuCau(MaPhieuYeuCau);
            lst = this.GetCHI_TIET_PYC(dt);

            return lst;
        }

        public int usp_InsertCHI_TIET_PYC(CHI_TIET_PYC obj)
        {
            int iResult = 0;
            string spName = "usp_InsertCHI_TIET_PYC";

            try
            {
                SqlParameter[] par = new SqlParameter[10];
                par[0] = new SqlParameter("@MaChiTiet", obj.MaChiTiet);
                par[1] = new SqlParameter("@MaThietBi", obj.MaThietBi);
                par[2] = new SqlParameter("@MaPhieuYeuCau", obj.MaPhieuYeuCau);
                par[3] = new SqlParameter("@TinhTrang", obj.TinhTrang);
                par[4] = new SqlParameter("@SoLuong", obj.SoLuong);
                par[5] = new SqlParameter("@NgayGiaoHang", obj.NgayGiaoHang);
                par[6] = new SqlParameter("@NguoiTao", obj.NguoiTao);
                par[7] = new SqlParameter("@NgayTao", obj.NgayTao);
                par[8] = new SqlParameter("@MaTrangThai", obj.oTRANG_THAI_VTPT.MaTrangThai);
                par[9] = new SqlParameter("@GhiChu", obj.GhiChu);

                if (SqlHelper.IsHaveTran())
                    iResult = SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, par);
                else
                    iResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, spName, par);

            }
            catch (Exception ex)
            {
            }
            return iResult;
        }

        public int usp_UpdateCHI_TIET_PYC(CHI_TIET_PYC obj)
        {
            int iResult = 0;
            string spName = "usp_UpdateCHI_TIET_PYC";

            try
            {
                SqlParameter[] par = new SqlParameter[10];
                par[0] = new SqlParameter("@MaChiTiet", obj.MaChiTiet);
                par[1] = new SqlParameter("@MaThietBi", obj.MaThietBi);
                par[2] = new SqlParameter("@MaPhieuYeuCau", obj.MaPhieuYeuCau);
                par[3] = new SqlParameter("@TinhTrang", obj.TinhTrang);
                par[4] = new SqlParameter("@SoLuong", obj.SoLuong);
                par[5] = new SqlParameter("@NgayGiaoHang", obj.NgayGiaoHang);
                par[6] = new SqlParameter("@NguoiTao", obj.NguoiTao);
                par[7] = new SqlParameter("@NgayTao", obj.NgayTao);
                par[8] = new SqlParameter("@MaTrangThai", obj.oTRANG_THAI_VTPT.MaTrangThai);
                par[9] = new SqlParameter("@GhiChu", obj.GhiChu);


                if (SqlHelper.IsHaveTran())
                    iResult = SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, par);
                else
                    iResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, spName, par);

            }
            catch (Exception ex)
            {
            }
            return iResult;
        }


        #endregion Public methods
    }
}
